'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 发放伤害排行奖励，重置BOSS状态
 * @author agl
 */
var moment = require('moment');
var async = require('async');
var unionBossService = require('../../services/unionBossService.js');
var unionBossModel = require('../../models/unionBossModel.js');
var roleUnionBossModel = require('../../models/roleUnionBossModel.js');

module.exports = function (callback) {
    unionBossModel.find({}, function (err, union_boss_list) {
        async.eachSeries(union_boss_list, function (union_boss_model, next) {
            _resetOneUnionBoss(union_boss_model, next);
        }, function (err) {
            if (!!err) {
                console.error('----联盟BOSS定时任务出错:' + err);
            }

            if (typeof (callback) == 'function') {
                callback();
            }
        });
    });
};

var _resetOneUnionBoss = function (union_boss_model, next) {
    console.log('-------- reset union boss:', union_boss_model.union_id);
    // 发放排名奖励
    async.waterfall(
        [
            function (cb) {
                unionBossService.doDamageRankReward(union_boss_model, cb);
            },
            function (cb) {
                // 重置BOSS
                union_boss_model.resetBoss();
                union_boss_model.save(cb);
            },
            function (cb) {
                roleUnionBossModel.removeAll(cb);
            }
        ],
        function (err) {
            if (!err) {
                console.error('----联盟BOSS定时任务--发放BOSS排行奖励失败:' + err);
            }
            next();
        }
    );
};
